package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVO;
import com.atguigu.eduservice.entity.chapter.VideoVO;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author JavaPorject
 * @since 2022-04-12
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;//注入小节service

    // 依照课程Id来查询所有的章节信息
    @Override
    public List<ChapterVO> getChapterVideoByCourseId(String courseId) {
        // 1 先查询课程所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        // 2 查询其中所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);


        // 3 遍历查询的章节 利用list集合进行封装
        List<ChapterVO> finalList = new ArrayList<>();
        // 遍历查询章节list集合
        for (EduChapter eduChapter : eduChapterList) {
            // 每个章节
            // eduChapter对象值复制到ChapterVo里面
            ChapterVO chapterVo = new ChapterVO();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            // 把chapterVo放到最终list集合
            finalList.add(chapterVo);

            // 创建集合，用于封装章节的小节
            List<VideoVO> videoList = new ArrayList<>();
            // 4 遍历查询小节list集合，进行封装
            for (EduVideo eduVideo : eduVideoList) {
                // 得到每个小节
                // 判断：小节里面chapterId和章节里面id是否一样
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    // 进行封装
                    VideoVO videoVo = new VideoVO();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    // 放到小节封装集合
                    videoList.add(videoVo);
                }
            }
            // 把封装之后小节list集合，放到章节对象里面
            chapterVo.setChildren(videoList);
        }
        return finalList;
    }

    // 删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        // 根据章节id查询小节表 如果查询出来有数据 则不能进行删除
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("chapter_id",chapterId);
        int count = videoService.count(wrapperVideo);
        // 判断
        if (count > 0) {
            // 如果查询出来有数据 则不能进行删除
            throw new GuliException(20001, "章节下面有小节，不能删除");

        }else {
            // 如果查询出来没有数据 则可以进行删除
            int res = baseMapper.deleteById(chapterId);
            return res > 0;
        }
    }

    //2 根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
